<?php

/**
 * @file classes/manager/form/SDTKForm.inc.php
 *
 * Copyright (c) 2003-2012 John Willinsky
 * Distributed under the GNU GPL v2. For full terms see the file docs/COPYING.
 *
 * @class SDTKForm
 * @ingroup manager_form
 *
 * @brief Form for creating and modifying journal SDTKs.
 */

// $Id$


import('lib.pkp.classes.form.Form');

class SDTKForm extends Form {

	/** @var $SDTKId int The ID of the SDTK being edited */
	var $SDTKId;

	/** @var $includeSDTKEditor object Additional SDTK editor to
	 *       include in assigned list for this SDTK
	 */
	var $includeSDTKEditor;

	/** @var $omitSDTKEditor object Assigned SDTK editor to omit from
	 *       assigned list for this SDTK
	 */
	var $omitSDTKEditor;

	/** @var $SDTKEditors array List of user objects representing the
	 *       available SDTK editors for this journal.
	 */
	var $SDTKEditors;

	/**
	 * Constructor.
	 * @param $journalId int omit for a new journal
	 */
	function SDTKForm($SDTKId = null) {
		parent::Form('acctKT/SDTKForm.tpl');

		$journal =& Request::getJournal();
		$this->SDTKId = $SDTKId;

		// Validation checks for this form
		$this->addCheck(new FormValidatorLocale($this, 'matk', 'required', 'Phai co Ma TK!'));
		$this->addCheck(new FormValidatorLocale($this, 'mant', 'required', 'Phai co Ten TK'));
		$this->addCheck(new FormValidatorPost($this));
		

		// Get a list of SDTK editors for this journal.
		$roleDao =& DAORegistry::getDAO('RoleDAO');
		$this->SDTKEditors =& $roleDao->getUsersByRoleId(ROLE_ID_SDTK_EDITOR, $journal->getId());
		$this->SDTKEditors =& $this->SDTKEditors->toArray();
		
	}

	/**
	 * When displaying the form, include the specified SDTK editor
	 * in the assigned list for this SDTK.
	 * @param $SDTKEditorId int
	 */
	function includeSDTKEditor($SDTKEditorId) {
		foreach ($this->SDTKEditors as $key => $junk) {
			if ($this->SDTKEditors[$key]->getId() == $SDTKEditorId) {
				$this->includeSDTKEditor =& $this->SDTKEditors[$key];
			}
		}
	}

	/**
	 * When displaying the form, omit the specified SDTK editor from
	 * the assigned list for this SDTK.
	 */
	function omitSDTKEditor($SDTKEditorId) {
		foreach ($this->SDTKEditors as $key => $junk) {
			if ($this->SDTKEditors[$key]->getId() == $SDTKEditorId) {
				$this->omitSDTKEditor =& $this->SDTKEditors[$key];
			}
		}
	}

	/**
	 * Get the names of fields for which localized data is allowed.
	 * @return array
	 */
	function getLocaleFieldNames() {
		$SDTKDao =& DAORegistry::getDAO('SDTKDAO');
		return $SDTKDao->getLocaleFieldNames();
	}

	/**
	 * Display the form.
	 */
	function display($templateMgr) {
		$journal =& Request::getJournal();
		$templateMgr =& TemplateManager::getManager();

		$templateMgr->assign('SDTKId', $this->SDTKId);
		
		parent::display();
	}

	/**
	 * Initialize form data from current settings.
	 */
	function initData() {
		$journal =& Request::getJournal();
		if (isset($this->SDTKId)) {
			$SDTKDao =& DAORegistry::getDAO('SDTKDAO');
			$SDTK =& $SDTKDao->getSDTK($this->SDTKId, false, false);
			if ($SDTK == null) {
				unset($this->SDTKId);
			} else {
				$this->_data = array(
					'matk' => $SDTK->getmatk(), // Localized
					'tentk' => $SDTK->gettentk(), // Localized
				);
			}
		}
	}

	/**
	 * Assign form data to user-submitted data.
	 */
	function readInputData() {
		$this->readUserVars(array('matk', 'mant', 'madv', 'dknont', 'dknovn'));
	}

	/**
	 * Save SDTK.
	 */
	function execute($nam) {

		$SDTKDao =& DAORegistry::getDAO('SDTKDAO');

		if (isset($this->SDTKId)) {
			$SDTK =& $SDTKDao->getSDTK($this->SDTKId, false, false);
		}

		if (!isset($SDTK)) {
			$SDTK = new SDTK();
		}

		$SDTK->setmatk($this->getData('matk'), null); // Localized
		$SDTK->setmant($this->getData('mant'), null); // Localized

		
		if (isset($this->SDTKId)) {
			$SDTKDao->updateSDTK($SDTK);
			$SDTKId = $SDTK->getId();

		} else {

			$SDTKId = $SDTKDao->insertSDTK($SDTK, $nam);
			
		}

	}
}

?>
